<template>
	<view class="page">
		<view>
			<view class="">
				<!-- <cu-custom>
					<block style="color: #777777;font-size: 32rpx;" slot="content">登录</block>
				</cu-custom> -->
			</view>
		</view>

		<view class="logo-banner">
			<image class="login-image" src="https://www.cetczhxf.com/mp3/login_bg.png"></image>
		</view>
		<view class="logo-box">
			<image class="logo" src="https://www.cetczhxf.com/mp3/logo.png"></image>
		</view>
		<view class="logo-title">
			哈密市智慧消防
		</view>
		<view class="login-box" @getuserinfo="appLoginWx"
		open-type="getUserInfo">
			<button class="login-btn" @getuserinfo="appLoginWx" open-type="getUserInfo">微信一键登录</button>
		</view>
		<view class="register" @click="reg">用户注册</view>
		<!-- <view>
			<view style="text-align: center;margin-top: 40rpx;color: #1890FF;">测试内置账号</view>
			<view class="flex flex-wrap" style="margin: 0 40rpx;">
				<button style="margin: 20rpx 10rpx;" class="cu-btn margin-tb-sm sm bg-orange" @click="goA('1')">监管部门小程序</button>
				<button style="margin: 20rpx 10rpx;" class="cu-btn margin-tb-sm sm bg-orange" @click="goA('2')">检测企业小程序</button>
				<button style="margin: 20rpx 10rpx;" class="cu-btn margin-tb-sm sm bg-orange" @click="goA('3')">维保小程序</button>
				<button style="margin: 20rpx 10rpx;" class="cu-btn margin-tb-sm sm bg-orange" @click="goA('4')">业主企业小程序</button>
			</view>
		</view> -->

		<!-- <view class="flex flex-direction">
			<button class="cu-btn margin-tb-sm lg my-btn" @getuserinfo="appLoginWx" open-type="getUserInfo">授权登录</button>
			<text class="reg" @click="reg">用户注册</text>

		</view> -->

		<!-- 弹出授权 -->
		<view class="cu-modal" :class="modalName == 'DialogModal1' ? 'show' : ''">
			<view class="cu-dialog">
				<view class="padding-xl">
					<view class="bind-tip">绑定手机号码</view>
					<text>请先绑定手机号码再进行操作</text>
					<button class="cu-btn margin-tb-sm lg my-btn-1" @getphonenumber="onGetPhoneNumber"
						open-type="getPhoneNumber">绑定手机号码</button>
				</view>
			</view>
		</view>
		<!-- 弹出授权 -->
		<view class="cu-modal" :class="modalName1 == 'DialogModal1' ? 'show' : ''">
			<view class="cu-dialog">
				<view class="padding-xl">
					<view class="bind-tip">消息订阅</view>
					<text>请先订阅消息再进行操作</text>
					<button class="cu-btn margin-tb-sm lg my-btn-1" @click="registMessage">微信用户一键登录</button>
				</view>
			</view>
		</view>
		<message />
	</view>
</template>

<script>
import WXBizDataCrypt from "@/common/WXBizDataCrypt.js"
import * as constants from '@/common/config.js';

export default {
	created: function () {
		try {
			const value = uni.getStorageSync('token');
			if (value) {
				getApp().globalData.role = uni.getStorageSync('page')
				uni.reLaunch({
					url: "../index/index?id=1"
				});
			}
		} catch (e) {
			// error
		}
	},
	data() {
		return {
			code: '',
			openid: '',
			session_key: '',
			encryptedData: '',
			iv: '',
			modalName: '',
			modalName1: '',
			phone: '',
			role: '',
			data: ''

		};
	},
	methods: {
		goA(index) {
			let phone = ''
			switch (index) {
				case '1':
					phone = "18999271727"
					getApp().globalData.role = 'A'
					break;
				case '2':
					phone = "17797826546"
					getApp().globalData.role = 'B'
					break;
				case '3':
					phone = "15809024803"
					getApp().globalData.role = 'C'
					break;
				case '4':
					phone = "13579979022"
					getApp().globalData.role = 'D'
					break;
			}
			// 获取token
			this.setToken(phone)
		},
		setToken(phone) {
			let that = this
			let app = getApp()
			app.globalData.request({
				url: 'wx/auth/getByPhone',
				data: { 'phone': phone, 'openid': that.openid }
			}).then(function (res) {
				// 登录成功，
				that.phone = phone
				if (res.data.code == 0) {
					if (res.data.data.data.userInfo) {
						that.role = res.data.data.data.userInfo.roleCode
						uni.setStorageSync('roleCode', res.data.data.data.userInfo.roleCode);
						uni.setStorageSync('mobile', res.data.data.data.userInfo.mobile);
						uni.setStorageSync('name', res.data.data.data.userInfo.name);
						uni.setStorageSync('ucId', res.data.data.data.userInfo.ucId);
						uni.setStorageSync('authorization', res.data.data.data.token);
						// TODO 根据rolecode跳转页面。
						if (res.data.data.data.userInfo.roleCode) {
							if (res.data.data.data.userInfo.roleCode.indexOf("WB") > -1) {
								getApp().globalData.role = 'C'
								app.globalData.setToken("page", 'C')
								uni.reLaunch({
									url: "../index/index?id=1"
								});
							} else if (res.data.data.data.userInfo.roleCode.indexOf("JC") > -1) {
								getApp().globalData.role = 'B'
								app.globalData.setToken("page", 'B')
								uni.reLaunch({
									url: "../index/index?id=1"
								});
							} else if (res.data.data.data.userInfo.roleCode.indexOf("XZ") > -1) {
								getApp().globalData.role = 'D'
								app.globalData.setToken("page", 'D')
								uni.reLaunch({
									url: "../index/index?id=1"
								});
							} else if (res.data.data.data.userInfo.roleCode.indexOf("XFZD") > -1
								|| res.data.data.data.userInfo.roleCode.indexOf("ZD") > -1
								|| res.data.data.data.userInfo.roleCode.indexOf("DD") > -1) {
								getApp().globalData.role = 'A'
								app.globalData.setToken("page", 'A')
								uni.reLaunch({
									url: "../index/index?id=1"
								});
							} else {
								that.modalName1 = ''
								uni.showToast({
									title: "用户无权限！",
									icon: 'error'
								})
							}

						} else {
							that.modalName1 = ''
							uni.showModal({
								title: "用户无权限",
								showCancel: false
							})
						}
					} else {
						that.modalName1 = ''
						uni.showToast({
							title: "用户无权限！",
							icon: 'error'
						})
					}
				}
			}).catch(function (e) {
				that.modalName1 = ''
				uni.showModal({
					title: "用户无权限",
					showCancel: false
				})
			})
		},
		showModal(e) {
			this.modalName = e.currentTarget.dataset.target
		},
		hideModal(e) {
			this.modalName = null
		},
		registMessage() {
			let self = this
			uni.requestSubscribeMessage({
				tmplIds: ['6d2uoEO84Rx-Shz7p60Z1IEublRBDD8f0nsiBknZgYI'],
				success(resp) {
					self.setToken(self.data.purePhoneNumber)
				}
			})
		},
		onGetPhoneNumber(e) {
			if (e.detail.errMsg == "getPhoneNumber:ok") {
				let encryptedData = e.detail.encryptedData
				let errMsg = e.detail.errMsg
				let iv = e.detail.iv
				let pc = new WXBizDataCrypt(constants.APPID, this.session_key);
				this.data = pc.decryptData(encryptedData, iv);
			}
			this.modalName = ''
			this.modalName1 = 'DialogModal1'
		},
		appLoginWx() {
			// 获取用户信息
			uni.getUserInfo({
				provider: 'weixin',
				lang: 'zh_CN',
				success: userInfo => {
					this.encryptedData = userInfo.encryptedData
					this.iv = userInfo.iv
					uni.login({
						provider: 'weixin',
						success: loginInfo => {
							this.wxLogin()
						}
					});
					this.modalName = 'DialogModal1'
				},
				fail: err => {
					console.log(err, 'err')
				}
			});
		},
		reg() {
			uni.navigateTo({
				url: '../reg/reg'
			});
		},
		getUserInfo(res) {
		},
		wxLogin() {
			let self = this
			uni.login({
				provider: 'weixin',
				success: function (code_res) {
					if (code_res.errMsg == "login:ok") {
						let code = code_res.code;
						let app = getApp()
						app.globalData.request({
							url: 'wx/auth/login_by_weixin',
							data: code
						}).then(function (res) {
							self.openid = res.data.data.data.openId
							self.session_key = res.data.data.data.sessionKey
						})
					}
				}
			})
		},
	}
}
</script>

<style scoped lang="scss">
.page {
	height: 100vh;
}

.logo-banner {
	width: 100%;
	height: 260px;
	margin: 0 auto;
	border-radius: 16rpx;
	border-radius: 16rpx;
}

.login-image {
	width: 100%;
	height: 260px;
	margin: 0 auto;
}
.logo-box {
	width: 100%;
	margin: -40px auto 0 auto;
	text-align: center;
}
.logo {
	width: 96px;
	height: 117px;
	margin: 0 auto;
}

.logo-title {
	margin: 40px auto 77px auto;
	font-size: 36px;
	font-weight: bold;
	line-height: 29px;
	letter-spacing: 0.39px;
	color: #3186FF;
	text-align: center;
}

.login-box {
	margin: 0 auto;
	width: 311px;
	height: 48px;
	background: #1A66FF;
	color: #fff;
	text-align: center;
	line-height: 48px;
	border-radius: 6px;

}

.register {
	color: #333333;
	font-size: 12px;
	margin-top: 20px;
	margin-right: 70px;
	text-align: right;
}

.my-btn {
	height: 94rpx;
	margin-top: 80rpx;
	background-image: linear-gradient(90deg, #FAB53A 4%, #EFA21A 99%);
	box-shadow: 0 0 12rpx 0 rgba(250, 181, 58, 0.50);
	border-radius: 24rpx;
	ont-family: PingFangSC-Medium;
	color: #FFFFFF;
	letter-spacing: 0;
	text-align: center;
}
.login-btn{
	line-height: 48px;
	color: #ffffff;
	background-color: #f8f8f800;
}
.my-btn-1 {
	height: 94rpx;
	background-image: linear-gradient(90deg, #FAB53A 4%, #EFA21A 99%);
	box-shadow: 0 0 12rpx 0 rgba(250, 181, 58, 0.50);
	border-radius: 24rpx;
	ont-family: PingFangSC-Medium;
	color: #FFFFFF;
	letter-spacing: 0;
	text-align: center;
	margin-bottom: 0;
}

.bind-tip {
	font-size: 38rpx;
	margin-bottom: 24rpx;
}

.reg {
	font-family: PingFangSC-Medium;
	font-size: 24rpx;
	color: #458DF5;
	letter-spacing: 0;
	text-align: center;
}
</style>
